package com.heima.article.service.impl;

import com.heima.article.service.ApArticleSearchService;
import com.heima.model.article.dtos.UserSearchDto;
import com.heima.model.article.pojos.ApAssociateWords;
import com.heima.model.article.pojos.ApHotArticles;
import com.heima.model.article.pojos.ApHotWords;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mappers.app.ApAssociateWordsMapper;
import com.heima.model.mappers.app.ApBehaviorEntryMapper;
import com.heima.model.mappers.app.ApHotWordsMapper;
import com.heima.model.mappers.app.ApUserSearchMapper;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserSearch;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.hbase.thirdparty.org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class ApArticleSearchServiceImpl implements ApArticleSearchService {

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Autowired
    private ApUserSearchMapper apUserSearchMapper;

    @Autowired
    private ApAssociateWordsMapper apAssociateWordsMapper;

    public ResponseResult getEntryId(UserSearchDto dto){
        ApUser user = AppThreadLocalUtils.getUser();
        // 用户和设备不能同时为空
        if(user == null && dto.getEquipmentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if(user!=null){
            userId = user.getId();
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.selectByUserIdOrEquipemntId(userId, dto.getEquipmentId());
        // 行为实体找以及注册了，逻辑上这里是必定有值得，除非参数错误
        if(apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(apBehaviorEntry.getId());
    }

    /**
     * 查询搜索记录
     * 根据前端传送dto进行搜索
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        if (dto.getPageSize() > 50 || dto.getPageSize() < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //先获取行为实体
        ResponseResult result = getEntryId(dto);
        if (result.getCode() != AppHttpCodeEnum.SUCCESS.getCode()){
            return result;
        }
        //查询搜索记录
        List<ApUserSearch> list = apUserSearchMapper.
                selectByEntryId((int)result.getData(), dto.getPageSize());

        return ResponseResult.okResult(list);
    }


    /**
     * 删除搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {
        //判断数据是否合法
        if (dto.getHisList() == null || dto.getHisList().size() < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取行为实体
        ResponseResult ret = getEntryId(dto);

        if (ret.getCode() != AppHttpCodeEnum.SUCCESS.getCode()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //获取ids
        List<Integer> ids = dto.getHisList().stream().map(ApUserSearch::getId).collect(Collectors.toList());

        //删除数据
        int rows = apUserSearchMapper.delUserSearch((Integer) ret.getData(),ids);

        return ResponseResult.okResult(rows);
    }

    /**
     * 清除搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult clearUserSearch(UserSearchDto dto) {
        ResponseResult ret = getEntryId(dto);
        if (ret.getCode() != AppHttpCodeEnum.SUCCESS.getCode()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        int rows = apUserSearchMapper.clearUserSearch((Integer) ret.getData());
        return ResponseResult.okResult(rows);
    }

    @Autowired
     private ApHotWordsMapper apHotWordsMapper;
    /**
     * 今日热词搜索
     * @param data
     * @return
     */
    @Override
    public ResponseResult hotKeywords(String data) {
        if (StringUtils.isEmpty(data)){
            data = DateFormatUtils.format(new Date(),"yyy-MM-dd");
        }
        List<ApHotWords> list = apHotWordsMapper.queryByHotDate(data);
        return ResponseResult.okResult(list);
    }



    /**
     * 联想搜索
     *
     * dto.getSearchWords() 用户搜索的词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult searchAssociate(UserSearchDto dto) {
        if (dto.getPageSize() < 0 || dto.getPageSize() > 50){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ResponseResult entryId = getEntryId(dto);
        List<ApAssociateWords> aw = apAssociateWordsMapper.selectByAssociateWords("%" + dto.getSearchWords() + "%", dto.getPageSize());
        return ResponseResult.okResult(aw);
    }

}
